package com.wlza.syslog;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.wlza.commons.PropertyUtils;
import com.wlza.sign.EncryptService;
import com.wlza.sign.SignatureService;
import com.wlza.utils.Base64Util;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.graylog2.syslog4j.SyslogConstants;
import org.graylog2.syslog4j.server.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Date;

public class Server {

    public static final Logger log = LoggerFactory.getLogger(Server.class);
    private static final String HOST = PropertyUtils.getProperty("syslog.server.host");
    private static final int PORT = Integer.parseInt(PropertyUtils.getProperty("syslog.server.port"));;

    private final static EncryptService encryptService = EncryptService.getInstance();
    private final static SignatureService signatureService = SignatureService.getInstance();

    private final Client client = new Client();

    public void init() throws InterruptedException {
        SyslogServerIF server = SyslogServer.getInstance(SyslogConstants.UDP);
        SyslogServerConfigIF config = server.getConfig();
        config.setHost(HOST);
        config.setPort(PORT);

        config.addEventHandler(new SyslogServerSessionEventHandlerIF() {

            @Override
            public void initialize(SyslogServerIF syslogServerIF) {

            }

            @Override
            public void destroy(SyslogServerIF syslogServerIF) {

            }

            @Override
            public Object sessionOpened(SyslogServerIF syslogServerIF, SocketAddress socketAddress) {
                return null;
            }

            @Override
            public void event(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, SyslogServerEventIF syslogServerEventIF) {
                System.out.println("receive from:" + socketAddress + "message" + JSON.toJSONString(syslogServerEventIF));
                if (syslogServerEventIF != null) {
                    try {
                        InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddress;
                        JSONObject object = new JSONObject();
                        // 先签名后加密
                        object.put("sign", signatureService.signature(syslogServerEventIF.getMessage()));
                        object.put("message",syslogServerEventIF.getMessage());
//                      object.put("message", encryptService.encrypt(syslogServerEventIF.getMessage()));
                        String ret = object.toJSONString();
                        System.out.println("send==>" + ret);
                        client.send(ret);
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }

            @Override
            public void exception(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, Exception e) {

            }

            @Override
            public void sessionClosed(Object o, SyslogServerIF syslogServerIF, SocketAddress socketAddress, boolean b) {

            }
        });
        SyslogServer.getThreadedInstance(SyslogConstants.UDP);
        while (true){
            Thread.sleep(10000);
        }
    }
}
